<?php
//
//  Init
//
require_once('config.php');
require_once('lib/class.publication.php');
require_once('lib/class.user.php');
require_once('lib/class.allowance.php');
require_once('lib/class.subscription.php');
require_once('lib/class.export-xls.php');
require_once('lib/class.report.php');
require_once('mailer/class.mailer.php');


$publications = Publication::get_publications();

foreach( $publications as $pub ) {
    if( $pub->publication_id == 0 || !isset($pub)) continue; // skip admin, or unset pubs
    /**
     * 
     * 
     * Loop through all pubs
     */    
    
    
    
    /**
     * 
     * 
     * Scrub subs per publication
     */
    $TABLE_PREFIX = $pub->table_name;
    $WEEK_ORIGIN = strtotime($pub->week_origin); //OUR ORIGIN WEEK IS SET AT THE FIRST WEEK OF 2007, BECAUSE 1 - MONDAY, 2 - TUESDAY, ETC.
    Subscription::scrub_subs($pub->publication_id);
    
    /**
     * 
     * 
     * Generate Labels and Totals if required.
     */
    if ( $pub->publication_day == date('N') ) {
        // This is this publications publication day.
        $totals_url = $labels_url = '';
        
        $report_arrays = Report::get_totals_report($pub->publication_id);
        
        if( ( isset($report_arrays) && !empty($report_arrays) ) ) {
            $totals_url = Report::save_report( 'weekly_totals' . get_weeknum(),  $report_arrays, 2);
        } 
        
        $report_arrays = Report::get_labels($pub->publication_id, 'active');
        if( ( isset($report_arrays) && !empty($report_arrays) ) ) {
            $labels_url = Report::save_report( "mailing_labels_For_WK" . get_weeknum() . get_weeknum(),  $report_arrays, 2);
        }     
        
        $mailer = new eggMailer( "Labels and Totals Report" ); 
    
        $mailer->validate_input_as( $_SERVER['SERVER_NAME'], 'Website');
        $mailer->validate_input_as( BASE_URL . $totals_url, 'TOTALS_URL');
        $mailer->validate_input_as( BASE_URL . $labels_url, 'LABELS_URL');
        $mailer->AddBCC( EMAIL, 'Shawn Wernig');
        $mailer->AddAddress( Publication::get_admin_email( $pub->publication_id ), 'Admin');
        
        if ( $mailer->send_mail("Labels and Totals Report", 'sub-report') );
    }
}





?>




